# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=libusbsio
pkgver=2.1.11
pkgrel=0
pkgdesc="Library for USB-HID communication over SPI, I2C or GPIO"
url="https://www.nxp.com/design/software/development-software/library-for-windows-macos-and-ubuntu-linux:LIBUSBSIO"
arch="all"
license="BSD-3-Clause"
makedepends="
	eudev-dev
	hidapi-dev
	libusb-dev
	linux-headers
	py3-build
	py3-gpep517
	py3-installer
	py3-setuptools
	py3-wheel
	"
checkdepends="py3-pytest"
subpackages="$pkgname-pyc py3-$pkgname:_py3:noarch"
source="https://www.nxp.com/downloads/en/libraries/libusbsio-$pkgver-src.zip"
options="!check" # Fails to load dynamic library
builddir="$srcdir/libusbsio-$pkgver-src"

prepare() {
	default_prepare

	# Remove prebuilt shared libs
	rm -r bin/*

	cd python/dist
	tar xvzf libusbsio-$pkgver.tar.gz
	# Remove prebuild shared libs from sdist tarball
	rm -r libusbsio-$pkgver/libusbsio/bin/*
}

build() {
	make

	cd python/dist/libusbsio-$pkgver
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

check() {
	cd python/dist/libusbsio-$pkgver
	python3 -m venv --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl
	.testenv/bin/python3 -m pytest
}

package() {
	install -Dm755 bin/linux_*/libusbsio.so -t "$pkgdir"/usr/lib/

	cd python/dist/libusbsio-$pkgver
	python3 -m installer --destdir="$pkgdir" .dist/*.whl

	local sitedir="$(python3 -c 'import site;print(site.getsitepackages()[0])')"
	install -dm755 "$pkgdir"/$sitedir/libusbsio/bin/linux_$CARCH
	ln -fsv /usr/lib/libusbsio.so "$pkgdir"/$sitedir/libusbsio/bin/linux_$CARCH/libusbsio.so

}

_py3() {
	pkgdesc="$pkgdesc (Python bindings)"

	amove usr/lib/python3*
}

sha512sums="
200717be44cdb2bb7cc7eeba29afb6aed8a88a72e7664377c1abf8c1d1f88f7de1ed05c9a7d6c960ae53e4b9420889c252d51fd807b1534396430004138518a0  libusbsio-2.1.11-src.zip
"
